package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.bumptech.glide.Glide;
import com.stark.cartoonutil.lib.BaseController;
import com.stark.cartoonutil.lib.CartoonsController;
import com.stark.cartoonutil.lib.utils.ImgUtil;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityAestheticComicBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileP2pUtil;

public class AestheticComicActivity extends BaseAc<ActivityAestheticComicBinding> {

    public static String imgPath;
    private Uri imgUri;
    private CartoonsController cartoonsController;
    private int defWidth = -1;
    private int defHeight = -1;
    private boolean hasOpenSpecialEffect;


    @Override
    protected int onCreate() {
        return R.layout.activity_aesthetic_comic;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAestheticComicBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivReplacePicture.setOnClickListener(this);
        mDataBinding.ivSavePicture.setOnClickListener(this);

        mDataBinding.ivImgCompare.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!hasOpenSpecialEffect) return true;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mDataBinding.ivAestheticComicImg.setVisibility(View.VISIBLE);
                        mDataBinding.glImgView.setVisibility(View.GONE);
                        Glide.with(mContext).load(imgUri).into(mDataBinding.ivAestheticComicImg);
                        break;
                    case MotionEvent.ACTION_UP:
                        mDataBinding.ivAestheticComicImg.setVisibility(View.GONE);
                        mDataBinding.glImgView.setVisibility(View.VISIBLE);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivReplacePicture://切换图片
                Intent intent = new Intent(this, SelPicActivity.class);
                intent.putExtra(Content.Replace, "Replace");
                startActivity(intent);
                break;
            case R.id.ivSavePicture://保存图片
                saveCartoon();
                break;
        }
    }

    @Override
    protected void initData() {
        imgUri = UriUtils.file2Uri(FileUtils.getFileByPath(imgPath));
        cartoon();
    }

    private void saveCartoon() {
        showDialog(getString(R.string.save_ing));
        float aspectRatio = mDataBinding.glImgView.getWidth() * 1f / mDataBinding.glImgView.getHeight();
        cartoonsController.save(aspectRatio, new BaseController.ISaveCallback() {
            @Override
            public void onResult(@Nullable Uri fileUri) {
                dismissDialog();
                String srcPath = UriUtils.uri2File(fileUri).getPath();
                FileUtils.copy(srcPath, PathUtils.getFilesPathExternalFirst() + Content.myRecord + "/" + FileUtils.getFileName(srcPath));
                FileP2pUtil.copyPrivateImgToPublic(mContext, srcPath);
                ToastUtils.showShort(getString(R.string.save_success));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(HomeActivity.class);
                    }
                }, 500);
            }
        });
    }

    private void cartoon() {
        mDataBinding.ivAestheticComicImg.setVisibility(View.GONE);
        mDataBinding.glImgView.setVisibility(View.VISIBLE);
        //该控制器用于对图片进行动漫化处理
        cartoonsController = new CartoonsController(mContext, mDataBinding.glImgView);

        showDialog(getString(R.string.loading));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (defWidth < 0) {
                    defWidth = mDataBinding.glImgView.getWidth();
                    defHeight = mDataBinding.glImgView.getHeight();
                }
                Bitmap bitmap = ImgUtil.Companion.getCompressedBitmapByFilePath(imgPath, defWidth, defHeight);
                ViewGroup.LayoutParams layoutParams = mDataBinding.glImgView.getLayoutParams();
                layoutParams.width = bitmap.getWidth();
                layoutParams.height = bitmap.getHeight();
                mDataBinding.glImgView.setLayoutParams(layoutParams);
                mDataBinding.glImgView.setImageSrc(bitmap.copy(bitmap.getConfig(), bitmap.isMutable()), true);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        cartoonsController.applyCartoonsModel(new BaseController.IApplyModelCallback() {
                            @Override
                            public void onComplete() {
                                dismissDialog();
                                hasOpenSpecialEffect = true;
                            }
                        });
                    }
                }, 500);
            }
        }, 500);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //这里要释放资源
        if (cartoonsController != null) {
            cartoonsController.release();
            cartoonsController = null;
        }
    }

}